package tech.aistar.Conteoller;


import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.mapper.BusinessmsgMapper;
import tech.aistar.mapper.HrMapper;
import tech.aistar.mapper.JobMapper;
import tech.aistar.model.dto.CompanySearchDTO;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.*;
import tech.aistar.service.ICompanyService;
import tech.aistar.service.IJobService;

import java.util.List;


@Tag(name = "公司控制层接口", description = "关于公司的接口文档")
@Controller
@RequestMapping("company")
public class CompanyController {
    @Autowired
    private ICompanyService iCompanyService;

    @Autowired
    private BusinessmsgMapper businessmsgMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private HrMapper hrMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private CompanySearchDTO companySearchDTO;

    @GetMapping("all/{pageNum}")
    public String findAll(HttpServletRequest req, @PathVariable("pageNum") Integer pageNum) {
        PageInfo<Company> pageInfo = null;
        pageInfo = iCompanyService.findAllCompanyWithPage(pageNum, 12);
        req.setAttribute("pageInfo", pageInfo);
        CompanySearchDTO companySearchDTO = new CompanySearchDTO();
        req.setAttribute("companySearchDTO", companySearchDTO);
        if(redisTemplate.hasKey("customer")){
            Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
            req.setAttribute("customer",customer);
        }
        return "company/company";
    }

    @GetMapping("allMsg")
    public String findAllCompanyMsg(HttpServletRequest req, Integer pageNum) {
        PageInfo<Company> pageInfo = null;
        pageInfo = iCompanyService.findAllCompanyWithPage(pageNum, 12);
        req.setAttribute("pageInfo", pageInfo);
        return "company/companyMsg";
    }



    @GetMapping("allpage")
    public String pageGo(HttpServletRequest req, String pageNum, String city, String collectMoneyState, String peopleCounts) {
        Integer pageN= Integer.valueOf(pageNum);
        if (city!=""){
            companySearchDTO.setCity(city);
        }else {
            companySearchDTO.setCity(null);
        }
        if(collectMoneyState!=""){
            companySearchDTO.setCollectMoneyStatus(Integer.valueOf(collectMoneyState));
        }else {
            companySearchDTO.setCollectMoneyStatus(null);
        }
        if (peopleCounts!=""){
            companySearchDTO.setPeopleCounts(Integer.valueOf(peopleCounts));
        }else {
            companySearchDTO.setPeopleCounts(null);
        }
        PageInfo<Company> pageInfo = iCompanyService.findCompanyByCityCollectMPeopleC(pageN,companySearchDTO);
        req.setAttribute("pageInfo", pageInfo);
        return "company/companyMsg";
    }

    @GetMapping("all/msg")
    public String comMsg(HttpServletRequest req,String id){
        Integer companyId= Integer.valueOf(id);
        Company c= iCompanyService.getById(companyId);
        Businessmsg businessmsg=businessmsgMapper.getById(c.getBusinessmsgId());
        List<Job> allByCompanyId = jobMapper.getAllByCompanyId(c.getId());
        List<Hr> hrs=hrMapper.getByCompanyId(c.getId());
        if(redisTemplate.hasKey("customer")){
            Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
            req.setAttribute("customer",customer);
        }
        req.setAttribute("address",c.getAddress());
        req.setAttribute("hrCounts",hrs.size());
        req.setAttribute("jobCounts",allByCompanyId.size());
        req.setAttribute("businessmsg",businessmsg);
        req.setAttribute("companyMsg",c);
        return "companymsg/comMsg";
    }

//    @PostMapping("counts/{counts}")
//    public Result findCompanyByPeopelCounts(@PathVariable("counts") Integer counts) {
//        List<Company> companyList = iCompanyService.findCompanyByPeopleCounts(counts);
//        if (companyList == null) {
//            return new Result("500", "查询失败");
//        } else {
//            return new Result("200", "查询成功", companyList);
//        }
//    }


}
